import { pageTeacher } from '../../../../api/user'
import { pageAdvert } from '../../../../api/base'
const app = getApp()
Component({
  properties: {
    scrollHeight: {
      type: Number,
      value: 0
    }
  },
  data: {
    swiperData: [],
    swiper2Data: [],
    teacherParams: [],
    teachers: [],
    token: '',
    cateId: '',
    loading: false,
    isNoData: false,
    scrollY: true,
    direction: 'up'
  },
  options: {
    styleIsolation: 'apply-shared'
  },
  lifetimes: {
    attached() {
      this.init()
    }
  },
  attached() {
    this.init()
  },
  pageLifetimes: {
    show: function() {
      // 页面被展示
      this.setData({
        token: app.globalData.token || wx.getStorageSync('token')
      })
    }
  },
  methods: {
    scrollEvent() {
      const _this = this;
      const query = wx.createSelectorQuery().in(this);
      query.select('#findModule').boundingClientRect()
      query.exec(function(res) {
        const { windowHeight } = wx.getSystemInfoSync();
        const bottom = res[0].bottom - getApp().globalData.navHeight - 86;
        _this.setData({
          direction: windowHeight - bottom > 300 ? 'down' : 'up'
        })
      })
    },
    startScroll(){
      this.setData({ scrollY: true })
    },
    stopScroll(){
      this.setData({ scrollY: false })
    },
    init() {
      this.setData({
        token: app.globalData.token || wx.getStorageSync('token')
      })
      this.getPageAdvert()

    },
    getPageAdvert(){
      pageAdvert({
        condition: {
          categoryCode: 'wxmTeacherAdvert-1'
        },
        pager: {
          page: 1,
          pageSize: 10
        }
      }).then(res => {
        this.setData({
          swiper2Data: res.records
        })
      })
    },
    setTeachCondition(e) {
      if (!this.data.token) {
        this.setData({
          cateId: e.detail.subject
        })
      } else {
        this.getTeacherList(e.detail)
      }
    },
    bindLogin() {
      app.bindLogin().then(res => {
        this.setData({
          token: app.globalData.token || wx.getStorageSync('token')
        })
        this.getTeacherList(this.data.cateId)
      })
    },
    getTeacherList(params) {
      const condition = {
        state: params.state,
        subject: params.subject,
      }
      this.setData({
        laoding: true,
        isNoData: false
      })
      pageTeacher({
        condition: condition,
        pager: {
          page: 1,
          pageSize: 5
        }
      }).then(res => {
        this.setData({ loading: false })
        if (!res.records.length) {
          this.setData({ isNoData: true })
        }
        this.setData({
          teachers: res.records
        })
      })
    }
  }
})
